package com.usian.wemedia.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usian.apis.wemedia.WmUserControllerApi;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.wemedia.service.WmUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/user")
public class WmUserController implements WmUserControllerApi {


    @Autowired
    private WmUserService wmUserService;


    /**
     * 保存自媒体用户
     *
     * @param wmUser
     * @return
     */
    @PostMapping("/save")
    @Override
    public ResponseResult saveWmUser(@RequestBody WmUser wmUser) {
        boolean result = wmUserService.save(wmUser);
        if(result){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    /**
     * 根据自媒体用户名查询用户
     * @param name
     * @return
     */
    @GetMapping("/findByName/{name}")
    @Override
    public WmUser findByName(@PathVariable String name) {

        //LambdaQueryWrapper
        //        queryWrapper
        //Wrappers

        List<WmUser> userList = wmUserService.list(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, name));
        if(userList!=null &&userList.size()>0){
            return userList.get(0);
        }
        return null;
    }

    /**
     * admin端审核  根据文章中userid查询自媒体用户信息
     * @param id
     * @return
     */
    @Override
    @GetMapping("/findOne/{id}")
    public WmUser findWmUserById(@PathVariable Long id) {
        return wmUserService.getById(id);
    }
}
